
#import <UIKit/UIKit.h>
#import "APPTools.h"
#import "AFNetworking.h"
@implementation APPTools
#pragma mark - get请求不带缓存
+ (void)GETWithURL:(NSString *)urlStr
               par:(NSDictionary *)dic
           success:(void (^)(id))responseObj filed:(void (^)(NSError *))err
{

    AFHTTPSessionManager *man = [AFHTTPSessionManager manager];
    // 有的返回的数据格式，AFN不支持解析，所以我们要设置一下。让AFN支持。
    [man.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [man GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        responseObj(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        err(error);

    }];
}
#pragma mark - post请求不带缓存
+ (void)POSTWithURL:(NSString *)urlStr
                par:(NSDictionary *)dic
            success:(void (^)(id))response filed:(void (^)(NSError *))err
{
    AFHTTPSessionManager *man = [AFHTTPSessionManager manager];
    
    [man.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
     urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [man POST:urlStr parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        response(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        err(error);
    }];
}
#pragma mark - get请求带缓存
+ (void)CQCacheGETWithURL:(NSString *)urlStr
               par:(NSDictionary *)dic
           success:(void (^)(id))responseObj failed:(void (^)(NSError *))err
{
    AFHTTPSessionManager *man = [AFHTTPSessionManager manager];
    // 有的返回的数据格式，AFN不支持解析，所以我们要设置一下。让AFN支持。
    [man.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [man GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self saveResponseObjectToCaches:responseObject fileName:[NSString stringWithFormat:@"%ld.plist", (unsigned long) [urlStr hash]]];
        responseObj(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        id result = [self readResponseObjectFromCaches:[NSString stringWithFormat:@"%ld.plist", (unsigned long) [urlStr hash]]];
        err(error);
        responseObj(result);
    }];
}
#pragma mark - 自适应文字高度
+ (CGSize)labelRectWithSize:(CGSize)size LabelText:(NSString *)labelText Font:(UIFont *)font {
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
    CGSize actualsize = [labelText boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    return actualsize;
}
#pragma mark - 把responseObject对象保存到数据库
+ (void)saveResponseObjectToCaches:(id)responseObject fileName:(NSString *)fileName {
    [NSKeyedArchiver archiveRootObject:responseObject toFile:[self saveInCachesWithFileName:fileName]];
}
#pragma mark - 从数据库中把responseObject对象取出来
+ (id)readResponseObjectFromCaches:(NSString *)fileName {
    return [NSKeyedUnarchiver unarchiveObjectWithFile:[self saveInCachesWithFileName:fileName]];
}
#pragma mark - 获取Caches的路径并创建文件夹:文件夹名为dataSource
+ (NSString *)pathOfCaches {
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"dataSource"];
    
    NSFileManager *manage = [NSFileManager defaultManager];
    [manage createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    return path;
}
#pragma mark - 给定一个文件名存到文件夹名为dataSource的文件夹中此方法返回一个路径(存放文件的路径)
+ (NSString *)saveInCachesWithFileName:(NSString *)name {
    return [[self pathOfCaches] stringByAppendingPathComponent:name];
}

@end
